<?php

/**
 * Log class.
 *
 * @author Eugene Zabolotniy
 */
class Log
{
    public static $logFolder = 'log';

    public static function write($prefix, $vars)
    {
        $date = @date('Y-m-d');
        self::writeToFile(self::$logFolder . '/' . $prefix . $date . ".log", $vars);
    }

    public static function writeLog($vars)
    {
        $args = func_get_args();
        self::write('log_', $vars);
    }

    /**
     * Logs exception.
     *
     * @param Exception $ex
     * @param string $prefix
     */
    public static function writeException($ex, $prefix = 'ex_')
    {
        if (!$ex instanceof Exception) {
            self::writeTrace('Wrong using of writeException method.');
            return;
        }

        $story = gettype($ex) . " throwed in: " . $ex->getFile()
               . " file, on " . $ex->getLine()." line\n"
               . "Message: " . $ex->getMessage() . "\n"
               . "Trace:\n" . $ex->getTraceAsString() . "\n";

        if (isset($_REQUEST)) {
            $story .= "Request:\n" . print_r($_REQUEST, true) . "\n";
        }
        if (isset($_CONSTANTS)) {
            $story .= "Constants:\n" . print_r($_CONSTANTS, true) . "\n";
        }
        if (isset($_SESSION)) {
            $story .= "Session:\n" . print_r($_SESSION, true) . "\n";
        }
        if (isset($_COOKIE)) {
            $story .= "Cookie:\n" . print_r($_COOKIE, true) . "\n";
        }

        self::write($prefix, $story);
    }

    public static function writeTrace($data, $prefix = "trace_")
    {
        array_unshift($vars, 'trace_');

        $backtrace = debug_backtrace();
        $level = count($backtrace);
        $trace = "";
        foreach ($backtrace as $rec) {
            $trace .= "\n" . str_pad($level, 3, " ", STR_PAD_LEFT)
                   . ': ' . @$rec['file'] . ' (' . @$rec['line'] . ') - ';

            if (isset($rec['function'])) {
                if (isset($rec['class'])) {
                    $trace .= $rec['class'] . $rec['type'] . $rec['function'];
                } else {
                     $trace .= $rec['function'];
                }
                $trace .= '(';
                if (count($rec['args'])) {
                    foreach ($rec['args'] as $arg) {
                        if (is_object($arg)) {
                            $trace .= get_class($arg);
                        } elseif(is_array($arg)) {
                            $trace .= gettype($arg);
                        } elseif(is_bool($arg)) {
                            $trace .= $arg ? 'true' : 'false';
                        } else {
                            $arg  = str_replace("\n", ' ', (string) $arg);
                            $trace .= '"' . (strlen($arg) <= 30 ? $arg : substr($arg, 0, 25) . '[...]') . '"';
                        }
                        $trace .= ', ';
                    }
                    $trace = substr($trace, 0, -2);
                }
                $trace .= ")";
            }

            $level--;
        }

        self::write($prefix, $data);
    }

    public static function writeToFile($filename, $data)
    {
        if (!is_array($data)) {
            $data = array($data);
        }

        if (count($data) > 0){
            $date = @date('Y-m-d');
            $fh = fopen($filename, 'a');
            fwrite($fh, self::exactTime() . " >>> ");

            if (count($data) == 1 && is_string($data[0])) {
                fwrite($fh, $data[0] . "\n");
            } else {
                foreach ($data as $var){
                    fwrite($fh, "\n" . print_r($var, true) . "\n");
                }
            } 
            fclose($fh);
        }
    }

    public static function exactTime()
    {
        return @date('Y-m-d H:i:s:') . substr(microtime(), 2, 6);
    }
}
